package com.blog.servlet;

import java.io.IOException;
import java.util.Vector;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.blog.dao.TagDao;
import com.blog.dto.Tag;
import com.blog.dto.User;

public class TagServlet extends HttpServlet {
	
	protected void addTag(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		int user_id = Integer.parseInt(req.getParameter("user_id"));
		String tag_name = req.getParameter("tag_name");
		Tag tag = new Tag();
		tag.setUser_id(user_id);
		tag.setTag_name(tag_name);
		TagDao tagDao = new TagDao();
		if(tagDao.addTag(tag)) {
			findAllTag(req, resp);
			req.getRequestDispatcher("/admin/OKAndClose.html").forward(req, resp);
		}else {
			resp.getWriter().append("标签名称不合法!");
		}
		return ;
	}
	
	protected void delTag(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		int tag_id = Integer.parseInt(req.getParameter("tag_id"));
		TagDao tagDao = new TagDao();
		tagDao.delTagByTag_id(tag_id);
		findAllTag(req, resp);
		return ;
		
	}
	
	protected void findAllTag(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		Vector<Tag> vTags = new Vector<Tag>();
		User user = new User();
		HttpSession session = req.getSession();
		user = (User) session.getAttribute("user");
		int user_id = user.getUser_id();
		TagDao tagDao = new TagDao();
		vTags = tagDao.findTagByUser_id(user_id);
		session.setAttribute("allUserTag", vTags);
		return ;
	}
	protected void editTag(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		int tag_id = Integer.parseInt(req.getParameter("tag_id"));
		int user_id = Integer.parseInt(req.getParameter("user_id"));
		String tag_name = req.getParameter("tag_name");
		Tag tag = new Tag();
		tag.setUser_id(user_id);
		tag.setTag_name(tag_name);
		tag.setTag_id(tag_id);
		TagDao tagDao = new TagDao();
		if(tagDao.editTag(tag)) {
			findAllTag(req, resp);
			req.getRequestDispatcher("/admin/OKAndClose.html").forward(req, resp);
		}else {
			resp.getWriter().append("标签修改失败!");
		}
	}
	
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String act = null;
		act=req.getParameter("act");
		
		if(act==null || act.equals("")) act = "index";
		System.out.println("TagServlet act = "+act);
		
		switch (act) {
		case "add":
			addTag(req, resp);
			break;
		case "del":
			delTag(req, resp);
			break;
		case "edit":
			editTag(req, resp);
			break;
		case "findAllTag":
			findAllTag(req, resp);
			resp.sendRedirect(req.getContextPath()+"/admin/tag-list.jsp");
			break;
		case "findUserByName":
			
			break;

		default:
			findAllTag(req, resp);
//			页面重定向
			resp.sendRedirect(req.getContextPath()+"/admin/article-list.jsp");
//			页面转发
//			req.getRequestDispatcher("/index.jsp").forward(req, resp);
			break;
		}
				
	}

}
